package com.ququ.syb.mqhandler.service;

import com.alibaba.fastjson.JSONObject;
import com.ququ.common.result.ResultJson;
import com.ququ.syb.common.constant.PayChannelIdConstant;
import com.ququ.syb.common.constant.RabbitMqQueueNameConstant;
import com.ququ.syb.common.constant.ResultStatusConstant;
import com.ququ.syb.common.constant.WithdrawStatusConstant;
import com.ququ.syb.common.pojo.PayChannel;
import com.ququ.syb.common.pojo.PayOrder;
import com.ququ.syb.common.pojo.WithdrawOrder;
import com.ququ.syb.mqhandler.fegin.*;
import com.ququ.syb.mqhandler.rabbitmq.RabbitMQSender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class WithdrawChanpayService {

    private Logger logger = LoggerFactory.getLogger(WithdrawChanpayService.class);
    @Autowired
    private ChannelsRouterFegin channelsRouterFegin;
    @Autowired
    private WithdrawOrderFegin withdrawOrderFegin;
    @Autowired
    private WithdrawBusinessFegin withdrawBusinessFegin;
    @Autowired
    private PayChannelFegin payChannelFegin;
    @Autowired
    private PayOrderFegin payOrderFegin;
    @Autowired
    private RabbitMQSender rabbitMQSender;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public void doWithdraw(String tradenum){
        WithdrawOrder withdrawOrder = withdrawOrderFegin.queryWithdrawOrder(tradenum).getData();
        if (withdrawOrder == null){
            logger.error("{}提现订单不存在",tradenum);
            return;
        }
        if (withdrawOrder.getStatus() != WithdrawStatusConstant.AUDIT_SUCCESS){
            logger.error("{}提现订单不是审核完成状态，无法重复调用通道提现接口",tradenum);
            return;
        }
        PayChannel payChannel = payChannelFegin.getPayChannel(withdrawOrder.getPaychannelid()).getData();
        if (payChannel.getAllowwithdraw() != null && payChannel.getAllowwithdraw() == 0){
            logger.error("{}通道不允许提现操作，暂不处理",withdrawOrder.getPaychannelid());
            return;
        }
        ResultJson withdrawResult = null;
        switch (withdrawOrder.getPaychannelid()) {
            case PayChannelIdConstant.CHAN_QUICK_PROXYPAY_API:
                withdrawResult =channelsRouterFegin.withdraw(withdrawOrder.getAccountid(),
                    withdrawOrder.getPaychannelid(),
                    withdrawOrder.getTradenum(),
                    withdrawOrder.getAmount()-withdrawOrder.getCustomercommission(),
                    null, null, null, null, null, null, null, null, null, null, null, null);
                break;
            case PayChannelIdConstant.CHANPAY_QK_QUICK_D0_API:
            case PayChannelIdConstant.CHANPAY_QK_QUICK_D0_API2:
                String extparams = null;
                PayOrder payOrder = payOrderFegin.getPayOrder(withdrawOrder.getPaytradenum()).getData();
                Date today = new Date();
                if (!simpleDateFormat.format(payOrder.getCreatetime()).equals(simpleDateFormat.format(today))){
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("d0",0);
                    extparams = jsonObject.toString();
                }
                withdrawResult =channelsRouterFegin.withdraw(withdrawOrder.getAccountid(),
                        withdrawOrder.getPaychannelid(),
                        withdrawOrder.getTradenum(),
                        withdrawOrder.getAmount(),
                        null, null, null, null, null, null, null, null, null, null, null,
                        extparams);
                break;

        }
        if (withdrawResult.getStatus() == ResultStatusConstant.SUCCESS){
            //提现操作成功，等待回调
            withdrawOrder.setExcutetime(new Date());
            withdrawOrder.setStatus(WithdrawStatusConstant.ACCEPTING);
            withdrawOrderFegin.updateWithdrawOrder(withdrawOrder);
            //钱客需自动查单
            if (withdrawOrder.getPaychannelid().equals(PayChannelIdConstant.CHANPAY_QK_QUICK_D0_API) || withdrawOrder.getPaychannelid().equals(PayChannelIdConstant.CHANPAY_QK_QUICK_D0_API2))
                rabbitMQSender.send(RabbitMqQueueNameConstant.WITHDRAW_QUERY,tradenum);
        }else {
            //提现操作失败
            withdrawBusinessFegin.withdrawCallback(tradenum,0,withdrawResult.getMsg());
        }
    }
}
